package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.ClassificationDAO;
import com.diankangwo.domain.Classification;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 二级分类控制器
 *
 * @author xiaoyu
 */
@Api(description = "二级分类")
@Controller
public class ClassificationController {

    @Autowired
    private ClassificationDAO classificationDao;

    /**
     * 获取指定分类下的子分类列表
     *
     * @param typeId 一级分类ID
     * @return
     */
    @ApiOperation(value = "获取指定分类下的子分类列表", notes = "")
    @ApiImplicitParam(name = "typeId", value = "一级分类ID", required = true, dataType = "Integer")
    @ResponseBody
    @RequestMapping(value = "/classification/list", method = RequestMethod.GET)
    public String show(Integer typeId) {
        List<Classification> cftList = classificationDao.findByTypeId(typeId);
        String code = "2";
        String msg = "处理异常！";
        if (cftList != null) {
            code = "1";
            msg = "查询成功！";
        } else {
            code="1";
            msg = "未查询到分类信息！";
        }
        return resultMsg.mapToJson(code, msg, cftList);
    }

    /**
     * 按ID获取二级分类信息
     *
     * @param classId 二级分类ID
     * @return
     */
    @ApiOperation(value = "按ID获取二级分类信息", notes = "")
    @ApiImplicitParam(name = "classId", value = "二级分类ID", required = true, dataType = "Integer")
    @ResponseBody
    @RequestMapping(value = "/classification/findById", method = RequestMethod.GET)
    public String findById(Integer classId) {
        Classification cftList = classificationDao.find(classId);
        String code = "2";
        String msg = "处理异常！";
        if (cftList != null) {
            code = "1";
            msg = "查询成功！";
        } else {
            code="1";
            msg = "未查询到分类信息！";
        }
        return resultMsg.mapToJson(code, msg, cftList);
    }
}
